package com.wbt.jdbc;

import java.io.*;
import java.sql.*;
import java.util.Enumeration;
import java.util.ResourceBundle;

public class JDBCTestMysql {
    public static void main(String[] args) throws SQLException, IOException {
        ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
        String driver = bundle.getString("jdbc.driver");
        String url = bundle.getString("jdbc.url");
        String user = bundle.getString("jdbc.username");
        String passwd = bundle.getString("jdbc.password");

        CharArrayWriter cw = new CharArrayWriter();
        Connection connection = null;
        Statement statement = null;
        try {

            PrintWriter pw = new PrintWriter(cw);
            DriverManager.setLogWriter(pw);

            //Class.forName(driver);
            getdriver();
            connection = DriverManager.getConnection(url,user,passwd);
            statement = connection.createStatement();

            System.out.println(DriverManager.class.getClassLoader());
            System.out.println(com.mysql.cj.jdbc.Driver.class.getClassLoader());

            String sql = "select count(0) from user";

            String sqlUser = "select id,age,userName,`work` from user;";
            ResultSet set = statement.executeQuery(sqlUser);

            while (set.next()){
                System.out.println("编号："+set.getInt("id")+",名称："+ set.getString("userName")+",age："+set.getInt("age")+",work："+set.getString("work"));
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {

            System.out.println("=============================================================================");
            BufferedReader reader2
                    = new BufferedReader(new CharArrayReader(cw.toCharArray()));
            String rs2 = "";
            while ( (rs2 = reader2.readLine()) != null){
                System.out.println(rs2);
            }
            statement.cancel();
            connection.close();
        }

    }

    public static void getdriver(){

        Enumeration<Driver> driverEnum = DriverManager.getDrivers();
        //打印出所有驱动信息
        while(driverEnum.hasMoreElements()){
            System.out.println(driverEnum.nextElement());
        }
    }
}
